<template>
	<view>
		<uni-row class="demo-uni-row">
			<v-tabs @change="changetab" ref="vTab" v-model="current" :tabs="tabs" pills :fixed="false" style="border-top: 1px solid #dcdfe6;
			border-bottom: 1px solid #dcdfe6;" pillsBorderRadius='0rpx' pillsColor='#fff' activeColor='#2979ff' bgColor="#fff"
				:zIndex=0 :line-animation="false"></v-tabs>
			<view v-if="current ===0" style="width :99%; margin-left:0.5%">
				<image :style="{width:'100%',height:picHeight+'px'}" :src="src" mode="scaleToFill">
				</image>
			</view>
			<view v-if="current ===1" style="width :99%; margin-left:0.5%">
				<scroll-view :scroll-top="scrollTop" scroll-y="true" :style="{ height: picHeight+'px' }"
					style="background-color: #f2f2f2">
					<view v-for="(item,index) in Filelist ">
						<view style="padding-left: 1%;padding-top:4%;list-style-type : none;">
							<view style="font-size:20px" @click="fileLook(item.url)"><span
									style="font-size:20px">{{item.name}}</span>
							</view>
						</view>
					</view>
				</scroll-view>
			</view>
			<view v-if="current ===2" style="width :99.8%; margin-left:0.1%;background-color: #fff">
				<uni-search-bar v-model="chitName" radius="4" cancelButton="always" cancel-text="查询" placeholder="问题分类"
					@cancel="abnormalMangerHistoryClick" />
				<uni-table ref="table" style="height: calc(100vh - 130px);" :loading="Historyloading" border stripe
					emptyText="暂无更多数据">
					<uni-tr>
						<uni-th width="120px" align="center">零件号</uni-th>
						<uni-th width="90px" align="center">发现工序</uni-th>
						<uni-th width="100px" align="center">异常开出时间</uni-th>
						<uni-th width="100px" align="center">异常开出人</uni-th>
						<uni-th width="100px" align="center">实测值信息</uni-th>
						<uni-th width="100px" align="center">泡泡号/位置</uni-th>
						<uni-th width="100px" align="center">理论值/公差</uni-th>
						<uni-th width="100px" align="center">异常现象</uni-th>
						<uni-th width="100px" align="center">超差值</uni-th>
						<uni-th width="100px" align="center">操作</uni-th>
					</uni-tr>
					<uni-tr v-for="(item, index) in HistoryTable" :key="index">
						<uni-td>{{ item.partNumber }}</uni-td>
						<uni-td align="center">{{ item.jobCode }}</uni-td>
						<uni-td align="center">{{ item.createTime }}</uni-td>
						<uni-td align="center">{{ item.createUser }}</uni-td>
						<uni-td align="center">{{ item.inspectionValue }}</uni-td>
						<uni-td align="center">{{ item.chitName }}</uni-td>
						<uni-td align="center">{{ item.chitStandardvalue }}</uni-td>
						<uni-td align="center">{{ item.remark }}</uni-td>
						<uni-td align="center">{{ item.distanceValue }}</uni-td>
						<uni-td align="center">
							<view class="uni-group">
								<button class="uni-button" size="mini" type="primary"
									@click="handleAddCallBack(item)">图片信息</button>
							</view>
						</uni-td>
					</uni-tr>
				</uni-table>
				<view class="uni-pagination-box">
					<uni-pagination show-icon :page-size="History.pageSize" :current="History.pageIndex"
						:total="History.isPage" @change="Historyhange" />
				</view>
			</view>
		</uni-row>
		<uni-popup ref="inputDialog" type="dialog">
			<uni-popup-dialog type="info" title="图片" ref="inputClose" :showClose="false" confirmText="关闭"
				@confirm="dialogInputConfirm" :style="{width:viewWidth+'px'}">
				<view class="uni-margin-wrap">
					<swiper class="swiper" circular :autoplay="autoplay" :indicatorDots="indicatorDots">
						<swiper-item v-for="(item, index) in picList" :key="index">
							<view class="swiper-item">
								<image :style="{width:viewWidth+'px',height:'700px'}" :src="item.fileUrl" mode="widthFix">
								</image>
							</view>
						</swiper-item>
					</swiper>
				</view>
			</uni-popup-dialog>
		</uni-popup>
	</view>
</template>
<script>
	import {
		getViewPDF,
		findOrgCode
	} from '@/api/system/biaochu'
	import {
		getSimpleUID,
		SYpageByParamGetHistoryAbnormalItem,
		NTpageByParamGetHistoryAbnormalItem,
		SYfileManagerList,
		NTfileManagerList
	} from '@/api/system/task.js'
	export default {
		data() {
			return {
				indicatorDots: true,
				autoplay: true,
				picList: [],
				viewWidth: 600,
				Historyloading: false,
				current: 0,
				tabs: [
					'简图图片',
					'工艺文件',
					'历史异常',
				],
				scrollTop: 0, // 起始tab
				Filelist: [],
				src: "",
				picHeight: uni.getSystemInfoSync().windowHeight - 44, // 更新视口高度
				picWidth: uni.getSystemInfoSync().windowWidth,
				HistoryTable: [],
				History: {
					pageSize: 10,
					pageIndex: 1,
					isPage: 0
				},
				chitName: "",
				ite: {},
			}
		},
		onLoad(options) {
			let data = JSON.parse(options.data);
			this.ite = data.ite;
			this.looK(this.ite);
			this.ViewPDF(this.ite);
			this.HistoryData();
			console.log(this.ite)
		},
		methods: {
			dialogInputConfirm() {
				this.$refs.inputDialog.close();
			},
			handleAddCallBack(item) {
				console.log(item.inspectionValueId)
				if (this.ite.dpDb === "NT") {
					NTfileManagerList(item.inspectionValueId).then(res => {
						if (res.data.length > 0) {
							console.log(res.data)
							this.picList = res.data;
							this.$refs.inputDialog.open();
						} else {
							this.$modal.msgError("暂无图片")
						}
					})
				} else {
					SYfileManagerList(item.inspectionValueId).then(res => {
						if (res.data.length > 0) {
							console.log(res.data)
							this.picList = res.data;
							this.$refs.inputDialog.open();
						} else {
							this.$modal.msgError("暂无图片")
						}
					})
				}
			},
			Historyhange(e) {
				this.History.pageIndex = e.current;
				this.HistoryData();
			},
			abnormalMangerHistoryClick() {
				this.HistoryData();
			},
			HistoryData() {
				this.Historyloading = true;
				let moIds = [];
				moIds[0] = this.ite.moId;
				let data = {
					chitName: this.chitName,
					isPage: this.History.isPage,
					mateDesignnumber: this.ite.th,
					moIds: moIds,
					pageIndex: this.History.pageIndex,
					pageSize: this.History.pageSize,
				}
				console.log(data)
				if (this.ite.dpDb === "NT") {
					NTpageByParamGetHistoryAbnormalItem(data).then(res => {
						console.log(res.data)
						this.Historyloading = false;
						this.HistoryTable = res.data.records;
						this.History.isPage = res.data.total;

					})
				} else {
					SYpageByParamGetHistoryAbnormalItem(data).then(res => {
						console.log(res.data)
						this.Historyloading = false;
						this.HistoryTable = res.data.records;
						this.History.isPage = res.data.total;

					})
				}
			},
			looK(ite) {
				getSimpleUID(ite.mateCode, ite.dpDb === "NT" ? ite.dpDb : "SY").then(res => {
					if (res.status === 200) {
						if (res.data === null) {
							this.$modal.msgError("暂无简图")
						} else {
							this.src = "http://192.168.10.236:10001/FortuneWeb/offline/getFileByUid?uid=" + res
								.data
						}
					} else {
						this.$modal.msgError("" + res.msg)
					}
				});
			},
			changetab(index) {
				if (index === 1 && this.Filelist.length === 0) {
					this.$modal.msgError("暂无工艺文件")
				}
				if (index === 0 && this.src === "") {
					this.$modal.msgError("暂无简图")
				}
			},
			// 文件查看
			fileLook(item) {
				if (item != "") {
					// console.log("文件查看 ", item)
					uni.navigateTo({
						url: '/pages/web/pdf?fileurl=' + item
					})
				}
			},
			ViewPDF(workItem) {
				this.Filelist = []
				// console.log("获取文件参数", workItem.th, workItem.thVersion, workItem
				// 	.mo, workItem.mateCode, workItem.th, workItem.thVersion, workItem
				// 	.mo, workItem.mateCode, workItem.dpDb === "SY" ? 'FORTUNE' : "FORTUNA")
				getViewPDF(workItem.th, workItem.thVersion, workItem
					.mo, workItem.mateCode, workItem.dpDb === "SY" ? 'FORTUNE' : "FORTUNA").then(res => {
					// console.log("获取文件", res)
					for (var i = 0; i < res.data.length; i++) {
						var firstfile = {
							name: '',
							url: ''
						};
						if (res.data[i].indexOf("</span></li>") != -1) {
							const start = "<span style=\"font-size:24px\">"
							const end = "</span></li>"
							const startIndex = res.data[i].indexOf(start) + start.length;
							const endIndex = res.data[i].indexOf(end, startIndex);
							firstfile.name = res.data[i].substring(startIndex, endIndex).myReplace(
								"&nbsp",
								"  ")
							firstfile.url = ""
						} else {
							var dataname = JSON.parse(JSON.stringify(res.data[i]))
							var dataurl = JSON.parse(JSON.stringify(res.data[i]))
							const start = "style=\"font-size:24px\">"
							const end = "</a></li>"
							const startIndex = dataname.indexOf(start) + start.length;
							const endIndex = dataname.indexOf(end, startIndex);
							firstfile.name = dataname.substring(startIndex, endIndex).myReplace(
								"&nbsp", "  ")
							const urlstart = "<a href=\""
							const urlend = "\" target=\"_blank\" style=\"font-size:24px\">"
							const urlstartIndex = dataurl.indexOf(urlstart) + urlstart.length;
							const urlendIndex = dataurl.indexOf(urlend, urlstartIndex);
							firstfile.url = dataurl.substring(urlstartIndex, urlendIndex).replace(
								"Views/mes/showpdf.html?url=", "")
						}
						this.Filelist.push(firstfile);
					}
					//console.log("获取文件", this.Filelist)
				}).catch(error => {
					reject(error)
				})

			},
		}
	}
</script>

<style>
	.uni-margin-wrap {
		width: 500px;
	}

	.swiper {
		height: 300px;
	}

	.swiper-item {
		display: block;
		height: 300px;
		line-height: 300px;
		text-align: center;
	}

	.swiper-list {
		margin-top: 40px;
		margin-bottom: 0;
	}

	.uni-common-mt {
		margin-top: 60px;
		position: relative;
	}

	.info {
		position: absolute;
		right: 20px;
	}

	.uni-padding-wrap {
		width: 550px;
		padding: 0 100px;
	}
</style>